package com.study.web;

import com.study.bean.Person;
import com.study.bean.Persons;
import com.study.utils.SysUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import nvr.utils.NvrUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author study
 * @version 1.0
 * @description
 * @date 2020/6/7 10:29
 */
@Slf4j
@Scope("session")
@RestController
@RequestMapping("tools")
@Api(tags = "系统通用功能")
public class ToolsController {

    @GetMapping("date")
    public Ps date() {
        Ps ps = new Ps();
        ps.setLd(LocalDateTime.now());
        return ps;
    }

    static class Ps {
        private LocalDateTime ld;

        public LocalDateTime getLd() {
            return ld;
        }

        public void setLd(LocalDateTime ld) {
            this.ld = ld;
        }
    }

    @Resource
    Environment environment;
    @Value("${sys.config.pic.savepath}")
    String picPath;

    /**
     * * 在不知道文件名情况下获取上传文件
     *
     * @param request
     * @return
     */
    @ApiOperation("在不知道文件名情况下获取上传文件")
    @RequestMapping(value = "upfile", method = RequestMethod.POST)
    public String uploadOssFile(HttpServletRequest request, String is) {
        String picUrl = environment.getProperty("sys.config.pic.baseurl");
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = mr.getFileMap();
        Set<Map.Entry<String, MultipartFile>> entries = fileMap.entrySet();
        String url = null;
        for (Map.Entry<String, MultipartFile> entry : entries) {
            MultipartFile multipartFile = entry.getValue();
            String key = entry.getKey();
            url = multipartFile.getOriginalFilename();
            log.info("key-value|表单字段和文件名分别是|:{},{}", key, multipartFile.getOriginalFilename());
            try {
                // TODO 此方法无法自动创建不存在的文件夹
                // multipartFile.transferTo(new File(picPath +multipartFile.getOriginalFilename()));
                url = com.study.utils.SysUtils.getDateStrSpan_() + "/" + com.study.utils.SysUtils.getDate4() + multipartFile.getOriginalFilename();
                // TODO 可以自动创建不存在的文件夹
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(picPath + url));
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.format("%s/%s", picUrl, url);
    }

    @ApiOperation("上传文件")
    @PostMapping(value = "upfiles")
    public String upFile(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        log.info("原始文件名称{}", originalFilename);
        String url = com.study.utils.SysUtils.getDateStrSpan_() + "/" + com.study.utils.SysUtils.getDate4() + originalFilename;
        // TODO 可以自动创建不存在的文件夹
        file.transferTo(new File(picPath + url));
        return url;
    }

    @ApiOperation("文件下载")
    @GetMapping(value = "downfile")
    public void downFile(HttpServletRequest req, HttpServletResponse resp, String fileName) {
        String attach = NvrUtils.getResourcePath("attach");
        log.info("下载的文件是{}", fileName);
        SysUtils.downFile(req, resp,
                String.format("%s/%s", attach, fileName),
                fileName);
    }

    @ApiOperation("显示图片")
    @GetMapping(value = "showpic")
    public void showPic(HttpServletResponse resp) throws IOException {
        String attach = NvrUtils.getResourcePath("attach");
        FileUtils.copyFile(new File(attach, "1.jpg"), resp.getOutputStream());
    }

    @GetMapping("showperson")
    public Person person() {
        Persons person = new Persons();

        person.setBirthday(new Date());
        person.setAge(28);
        person.setLon(1287010494720266242L);
        return person;
    }

    private Map<String, String> result = new HashMap<String, String>();

    @ApiOperation("测试设置名字")
    @GetMapping(value = "name")
    public String nam(String name) {
        result.put("name", name);
        return SysUtils.getDateStrHms();
    }

    @ApiOperation("测试获取名字")
    @GetMapping(value = "getname")
    public String getName() {
        return result.getOrDefault("name", "defaultValue");
    }
}
